package com.hxm.appGenealogy.config;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxm.appGenealogy.Function.*;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.entity.*;
import com.hxm.appGenealogy.entryVo.FamilyMerge;
import com.hxm.appGenealogy.entryVo.MoveVo;
import com.hxm.appGenealogy.mapper.DynamicMapper;
import com.hxm.appGenealogy.mapper.imagePhMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW)
public class FamilyMergeConfig {

    @Autowired
    private imagePhMapper imagePhPer;

    @Autowired
    private FamilyFunction familyFunction;


    @Autowired
    private PeopleFunction peopleFunction;

    @Autowired
    private PeopleFunctionService peopleFunctionService;

    @Autowired
    private BrothersSistersPeopleFunction brothersSistersPeopleFunction;



    @Autowired
    private DynamicFunction dynamicFunction;
    /**
     * 相册数据的迁移;
     * @param familyMerge
     */
    public  void moveImage(FamilyMerge familyMerge){
        //获取原来相册当中照片信息的数据;
        List<imagePh> imagePhList = imagePhPer.selectList(new QueryWrapper<imagePh>().eq("fanily_id", familyMerge.getFamilyMergeId()));
        //将这些相册的数据迁移到新的家族的数据里面;
        imagePhList.forEach(t->{
            t.setFanilyId(familyMerge.getFamilyId());
            imagePhPer.updateById(t);
        });
    }

    /**
     *修改空间的使用的情况;
     */
    public void updateSpace(FamilyMerge familyMerge){
        Long familyMergeId = familyMerge.getFamilyMergeId();
        Family familyDel = (Family) familyFunction.selectByid(familyMergeId).getData();
        //已经使用了的空间的数据;
        Double useSpace = familyDel.getUseSpace();
        //可以使用的空间的数据;
        Double usableSpace = familyDel.getUsableSpace()-1024;
        Family familyUse = (Family)familyFunction.selectByid(familyMerge.getFamilyId()).getData();
        familyUse.setUseSpace(familyUse.getUseSpace()+useSpace);
        //获取原来家族购买的空间的数据;
        familyUse.setUsableSpace(familyUse.getUsableSpace()+usableSpace);
        //修改家族的空间的使用的情况;
        familyFunction.updateById(familyUse);
        //修改家族相册的数据;
        List<imagePh> fanily_id = imagePhPer.selectList(new QueryWrapper<imagePh>().eq("fanily_id", familyDel.getId()));
        fanily_id.forEach(t->{
            t.setPeopleId(familyMerge.getFamilyId());
            imagePhPer.updateById(t);
        });
        //设置迁移
        List<People> peopleList = peopleFunction.queryGeListString(JSON.toJSONString(new QueryWrapperVO().eq("family_id", familyMerge.getFamilyMergeId())));
        peopleList.forEach(p->{
            p.setFamilyId(familyMerge.getFamilyId());
            p.setJurisdiction(2);
            p.setFamilyMergeId(familyMerge.getFamilyMergeId());
            peopleFunction.updateByid(p);
        });
        //迁移动态的数据;
        List<Dynamic> dynamicList = dynamicFunction.selectGetListQuery(JSON.toJSONString(new QueryWrapperVO().eq("family_id", familyMerge.getFamilyMergeId())));
        dynamicList.forEach(d->{
            d.setFamilyId(familyMerge.getFamilyId());
            dynamicFunction.updateByid(d);
        });
        //将当前的家族进行删除;
        familyFunction.del(familyDel);
    }

    /**
     * 人员迁移;
     */
    public ResponDataVo MovePeople(MoveVo moveVo){
        People movePeople = peopleFunction.peopleGetByid(moveVo.getPeopleMoveId());
        People seatPeople = peopleFunction.peopleGetByid(moveVo.getSeatPeopleId());
        if(!movePeople.getGender().equals(seatPeople.getGender())){
            return new ResponDataVo("当前迁移的数据与tree站位的数据的性别不一致;");
        }
        if(movePeople.getName()!=null){
            seatPeople.setName(movePeople.getName());
        }
        if(movePeople.getSurname()!=null){
            seatPeople.setSurname(movePeople.getSurname());
        }
        if(movePeople.getAncestral()!=null){
            seatPeople.setAncestral(movePeople.getAncestral());
        }
        if(movePeople.getPresentAddress()!=null){
            seatPeople.setPresentAddress(movePeople.getPresentAddress());
        }
        if(movePeople.getBrief()!=null){
            seatPeople.setBrief(movePeople.getBrief());
        }
        if(movePeople.getNumber()!=null){
            seatPeople.setNumber(movePeople.getNumber());
        }
        if(movePeople.getIdCard()!=null){
            seatPeople.setIdCard(movePeople.getIdCard());
        }
        if(movePeople.getEducation()!=null){
            seatPeople.setEducation(movePeople.getEducation());
        }
        if(movePeople.getWorkUnit()!=null){
            seatPeople.setWorkUnit(movePeople.getWorkUnit());
        }
        if(movePeople.getPosition()!=null){
            seatPeople.setPosition(movePeople.getPosition());
        }
        seatPeople.setJurisdiction(2);
        if(movePeople.getOpenId()!=null){
            seatPeople.setOpenId(movePeople.getOpenId());
        }
        if(movePeople.getNickname()!=null){
            seatPeople.setNickname(movePeople.getNickname());
        }
        if(movePeople.getBirthdayTime()!=null){
            seatPeople.setBirthdayTime(movePeople.getBirthdayTime());
        }
        if(movePeople.getPassWorld()!=null){
            seatPeople.setPosition(movePeople.getPassWorld());
        }
        if(movePeople.getIsHavePit()!=null){
            seatPeople.setIsHavePit(movePeople.getIsHavePit());
        }
        if(movePeople.getAvater()!=null){
            seatPeople.setAvater(movePeople.getAvater());
        }
        try {
            peopleFunction.updateByid(seatPeople);
            peopleFunction.delPeopleByid(movePeople.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponDataVo("美女,服务器异常哦;");
        }
        return new ResponDataVo(true);

    }

}










